﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Models;
using System.Linq.Expressions;
using SBACN.WebSite.Code;

namespace SBACN.WebSite.Admin
{
	public partial class WebLinkEdit : BasePage
	{
		/// <summary>
		/// 查看新闻的系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "00080005";//友情连接，默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!AdminBasePage.CheckCodeRight(AdminBasePage.AdminInfo, SystemType))
			{
				AdminBasePage.RedirectWithMessage(AdminDefaultPage, "您没有访问当前页面的权限,请与系统管理员联系!");
				return;
			}

			this.HidID.Value = Request.QueryString["id"];
			if (!IsPostBack)
			{
				ShowList();
			}
		}

		protected void ShowList()
		{
			if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
			{
				var result = DAL.DALHelper.Instance.GetObject<TB_OutLink>("ID", Convert.ToInt32(Request.QueryString["id"]));
				if (result != null)
				{
					this.WebName.Text = result.LinkName;
					this.Addr.Text = result.LinkUrl;
					DropDownListMClass(result.Category);
				}
			}
			else
			{
				DropDownListMClass("");
			}

			
		}

		protected void DropDownListMClass(string Sel)
		{
			Expression<Func<TB_OutLink, bool>> fun = (p => p.Category == "");
			Expression<Func<TB_OutLink, DateTime>> order = (p => p.CreateDate);
			var result = DAL.DALHelper.Instance.GetList<TB_OutLink, DateTime>(fun, order, true);
			this.list.AppendDataBoundItems = true;
			this.list.DataSource = result;
			this.list.DataTextField = "LinkName";
			this.list.DataValueField = "ID";
			this.list.DataBind();
			if (Sel != "")
				this.list.SelectedIndex = this.list.Items.IndexOf(this.list.Items.FindByValue(Sel));

		}

		protected void Subbtn_Click(object sender, EventArgs e)
		{
			string fid = this.HidID.Value;
			if (string.IsNullOrWhiteSpace(fid))
			{// creat
				var NewObjLink = DAL.DALHelper.Instance.Create<TB_OutLink>();
				NewObjLink.LinkName = this.WebName.Text;
				NewObjLink.LinkUrl = this.Addr.Text;
				NewObjLink.Category = this.list.SelectedValue;
				NewObjLink.Type = this.SystemType;
				NewObjLink.CreateBy = AdminBasePage.AdminInfo.UserName;
				NewObjLink.CreateDate = DateTime.Now;
				DAL.DALHelper.Instance.Add<TB_OutLink>(NewObjLink);
				DAL.DALHelper.Instance.SaveChanges();
			}
			else
			{// update
				var result = DAL.DALHelper.Instance.GetObject<TB_OutLink>("ID", Convert.ToInt32(fid));
				result.LinkName = this.WebName.Text;
				result.LinkUrl = this.Addr.Text;
				result.Category = this.list.SelectedValue;
				DAL.DALHelper.Instance.Update<TB_OutLink>(result);
				DAL.DALHelper.Instance.SaveChanges();
				
			}
			RefreshNewsList();
		}
		private void RefreshNewsList()
		{
			string clientJs = "<script type=\"text/javascript\" language=\"javascript\">";
			clientJs += "if(window.opener && window.opener.RefreshNewsList) window.opener.RefreshNewsList();";
			clientJs += "window.close();</script>";
			Response.Write(clientJs);
			Response.End();
		}

	}
}